{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Plate ID</th>\n",
       "      <th>Registration State</th>\n",
       "      <th>Vehicle Make</th>\n",
       "      <th>Street Name</th>\n",
       "      <th>Vehicle Color</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>J58JKX</td>\n",
       "      <td>NJ</td>\n",
       "      <td>HONDA</td>\n",
       "      <td>43 ST</td>\n",
       "      <td>BK</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>KRE6058</td>\n",
       "      <td>PA</td>\n",
       "      <td>ME/BE</td>\n",
       "      <td>UNION ST</td>\n",
       "      <td>BLK</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>444326R</td>\n",
       "      <td>NJ</td>\n",
       "      <td>LEXUS</td>\n",
       "      <td>CLERMONT AVENUE</td>\n",
       "      <td>BLACK</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>F728330</td>\n",
       "      <td>OH</td>\n",
       "      <td>CHEVR</td>\n",
       "      <td>DIVISION AVE</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>FMY9090</td>\n",
       "      <td>NY</td>\n",
       "      <td>JEEP</td>\n",
       "      <td>GRAND ST</td>\n",
       "      <td>GREY</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Plate ID Registration State Vehicle Make      Street Name Vehicle Color\n",
       "0   J58JKX                 NJ        HONDA            43 ST            BK\n",
       "1  KRE6058                 PA        ME/BE         UNION ST           BLK\n",
       "2  444326R                 NJ        LEXUS  CLERMONT AVENUE         BLACK\n",
       "3  F728330                 OH        CHEVR     DIVISION AVE           NaN\n",
       "4  FMY9090                 NY         JEEP         GRAND ST          GREY"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = '../data/nyc-parking-violations-2020.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                 usecols=['Plate ID',  'Registration State',\n",
    "                        'Vehicle Make', 'Vehicle Color', 'Street Name'])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "Run `value_counts` on the `Vehicle Make` column, and look at some of the vehicle names. (There are more than 5,200 distinct makes, which almost certainly indicates that there is a lot of inconsistency in this data.) What problems do you see? Write a function that, given a value, cleans it up -- putting the name in all caps, removing punctuation, and standardizing whatever names you can, and then use the `apply` method to fix up the column. How many distinct vehicle makes are there when you're done?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5210\n",
      "4915\n"
     ]
    }
   ],
   "source": [
    "# I could have used regular expressions, but decided to make it a bit easier to follow\n",
    "\n",
    "import string\n",
    "\n",
    "def clean_name(one_string):\n",
    "\n",
    "    if not isinstance(one_string, str):\n",
    "        return one_string\n",
    "\n",
    "    output = ''\n",
    "    \n",
    "    for one_character in one_string.strip().upper():\n",
    "        if one_character in string.ascii_uppercase:\n",
    "            output += one_character\n",
    "\n",
    "    return output\n",
    "\n",
    "print(len(df['Vehicle Make'].value_counts()))\n",
    "df['Vehicle Make'] = df['Vehicle Make'].apply(clean_name)\n",
    "print(len(df['Vehicle Make'].value_counts()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "How standardized are the street names in system? What changes could you apply to improve things?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "W 110th St              2970\n",
       "110th St                2388\n",
       "E 110th St              2048\n",
       "WB 110TH AVE/BRINKER     922\n",
       "110th Ave                704\n",
       "                        ... \n",
       "O/F 77 EAST 110 ST         1\n",
       "C/O 110 RD                 1\n",
       "S/E C/O E 110 ST           1\n",
       "E/B 110 W 48 ST            1\n",
       "E 110  ST                  1\n",
       "Name: Street Name, Length: 73, dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Let's do some experiments to see how standardized things are\n",
    "\n",
    "# For example, it sometimes says E 110th St and sometimes says E 110 ST\n",
    "s = df['Street Name'].dropna()\n",
    "s[s.str.contains('110')].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SB BROADWAY @ 252ND     21939\n",
       "NB BROADWAY @ W 228T    13367\n",
       "BROADWAY                10771\n",
       "SB BROADWAY @ W 196T     6623\n",
       "NB BROADWAY @ W 120T     5691\n",
       "                        ...  \n",
       "S/B BWAY                    1\n",
       "BROADWAY PL                 1\n",
       "S/S BWAY                    1\n",
       "S/O 1350 BROADWAY           1\n",
       "N/E 220 BROADWAY            1\n",
       "Name: Street Name, Length: 181, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Sometimes it says BWAY and sometimes BROADWAY ...\n",
    "\n",
    "# So to clean things up, we would need to standardize whether we use st/nd/rd/th, and if/when\n",
    "# we abbreviate street names, and HOW we do that. Also, there is a separate column for the\n",
    "# cross street, so it shouldn't be in the \"Street Name\" column.  A mess!  (Or an opportunity...)\n",
    "\n",
    "s[s.str.contains('BWAY') | s.str.contains('BROADWAY')].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "Would you need to clean up the `Registration State` column? Why or why not?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NY    9753643\n",
       "NJ    1096110\n",
       "PA     338779\n",
       "FL     174056\n",
       "CT     165205\n",
       "       ...   \n",
       "PE         18\n",
       "SK          8\n",
       "MX          7\n",
       "NT          3\n",
       "YT          2\n",
       "Name: Registration State, Length: 68, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We have 68 \"states,\" which includes Canadian provinces and some other countries\n",
    "# So this seems pretty reasonsable, although perhaps some additional cleanup is needed.\n",
    "df['Registration State'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
